import logging
from common.all_path import *

class Mylog:
    def do_log(self, msg, level):
        # 创建容器
        my_log = logging.getLogger()
        # 设定等级
        my_log.setLevel("DEBUG")
        # 设置输出格式
        f = logging.Formatter('%(asctime)s - %(levelname)s - %(funcName)s-----日志信息：%(message)s')
        # 创建输出渠道
        ch = logging.StreamHandler()  # 指定输出到控制台
        ch.setLevel("DEBUG")
        ch.setFormatter(f)
        #         fh=logging.FileHandler(r"C:\Users\HS\eclipse-workspace\ExcelLibrary\simple\test.txt",encoding='utf-8')

        fh = logging.FileHandler(path_log)  # 指定输出到指定文件
        fh.setLevel("DEBUG")
        fh.setFormatter(f)
        # 指定输出渠道
        my_log.addHandler(ch)
        my_log.addHandler(fh)
        if level == "DEBUG":
            my_log.debug(msg)
        if level == "INFO":
            my_log.info(msg)
        if level == "WARN":
            my_log.warning(msg)
        if level == "ERROR":
            my_log.error(msg)
        # 关闭渠道
        my_log.removeHandler(ch)
        my_log.removeHandler(fh)

    def debug(self, msg):
        self.do_log(msg, "DEBUG")

    def info(self, msg):
        self.do_log(msg, "INFO")

    def warn(self, msg):
        self.do_log(msg, "WARN")

    def error(self, msg):
        self.do_log(msg, "ERROR")

if __name__ == '__main__':
    log = Mylog()
    # log.debug("test20200630")
    log.error("guo")